const Promise = require('bluebird');

/**
 * 代理结算
 */
exports.settle = function ({body, user_id}, {models: {Users, TaokeOrders}, cache: {settings, dicts}, controller}) {
    let {id} = body;
    if (!id) {
        return {success: false, message: '参数错误'};
    }
    return Users.findOne({where: {id: id, level: ['Partner', 'Agency'], partner_id: user_id}}).then(function (user) {
        if (!user) {
            return {success: false, message: '用户可能不是代理，或没有权限操作'}
        }
        return TaokeOrders.agencySettleV2(user);
    });
}

/**
 * 批量代理结算
 */
exports.batchSettle = function ({body, user_id}, {models: {Users, TaokeOrders}, cache: {settings, dicts}, controller}) {
    return Users.findOne({where: {id: user_id, level: ['Partner']}}).then(function (user) {
        if (!user) {
            return {success: false, message: '用户不是合伙人，或没有权限操作'}
        }
        return TaokeOrders.agencyBatchSettleV2(user);
    });
}